package com.leinao.browse.controller;

import com.alibaba.fastjson.JSON;
import com.leinao.browse.model.BrowseRecord;
import com.leinao.browse.service.BrowseRecordService;
import com.leinao.commons.Response;
import com.leinao.constant.Constant;
import com.leinao.login.dto.LoginResultDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @Description 功能浏览记录控制层
 * @author lizi
 * @date 2019/03/23 下午6:48
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class BrowseRecordController {

	private static Logger logger = LoggerFactory.getLogger(BrowseRecordController.class);

	@Autowired
	private BrowseRecordService browseRecordService;

	@ApiOperation(value = "获取功能浏览记录信息", notes = "获取功能浏览记录信息")
	@GetMapping(value = "/browseRecord/get")
	public Response<BrowseRecord> getBrowseRecord(@ApiParam(value = "功能浏览类型", required = true) @RequestParam Integer type,
                                                  HttpServletRequest request) {
		logger.info("getBrowseRecord param  type: {}", type);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		return browseRecordService.getBrowseRecordByTypeAndUserId(type, userInfo.getUserId());
	}

    @ApiOperation(value = "添加功能浏览记录信息", notes = "添加功能浏览记录信息")
    @PostMapping(value = "/browseRecord/insert")
    public Response<Long> insertBrowseRecord(@ApiParam(value = "功能浏览类型", required = true) @RequestParam Integer type,
                                             HttpServletRequest request) {
        logger.info("insertBrowseRecord param  type: {}", type);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return browseRecordService.addBrowseRecord(type, userInfo.getUserId());
    }
}
